<template>
    <div id="advices">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
            <el-form :inline="true" :model="AdvData">
                <el-form-item>
                    <el-input v-model="AdvData.title" placeholder="请输入要搜索的内容"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="PostAdvices">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--table-->
        <el-table :data="AdvIty" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column label="标题">
                <template slot-scope="scope">{{scope.row.title}}</template>
            </el-table-column>
            <el-table-column prop="method" label="消息类型"></el-table-column>
            <el-table-column label="扩展">
                <template slot-scope="scope">
                    <p class="adv-text">{{scope.row.extra}}</p>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size" :total="total" style="float:right;"></el-pagination>
        </el-col>

        <!--新增-->
        <el-dialog title="添加消息" v-model="addFormVisible" :close-on-click-modal="false" top="5%" size="large">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="addForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <UEditor @ready="editorReady" style=""></UEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--编辑-->
        <el-dialog title="编辑消息" v-model="editFormVisible" :close-on-click-modal="false" :before-close="abolish" top="5%" size="large">
            <el-form :model="editForm" label-width="80px"  ref="editForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="editForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <UEditor @ready="editorReadyEdit"></UEditor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="abolish">取消</el-button>
                <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import util from '../../common/js/util';
    import {advicesList, advicesDel, advicesNew, advicesEdit, advicesSings} from '../../api/api';
    import UEditor from '../../components/ueditor/index';
    export default {
        data() {
            return {
                AdvIty: [],
                total: 0,
                page: 1,
                size: 15,
                AdvData: {
                    title: ''
                },
                listLoading: false,
                sels: [],
                /*新增*/
                addFormVisible: false,
                addLoading: false,
                addFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'},
                },
                addForm: {
                    title: '',
                    content: ''
                },
                /*编辑*/
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                //编辑界面数据
                editForm: {
                    title: '',
                    content: ''
                },
                ueEdit: null,
                ueAdd: null,
            }
        },
        methods: {
            editorReady (editorInstance) {
                this.ueAdd = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            editorReadyEdit (editorInstance) {
                this.ueEdit = editorInstance;
                editorInstance.setContent('');
                editorInstance.addListener('contentChange', () => {
                    console.log('内容发生了变化：', editorInstance.getContent());
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.PostAdvices();
            },
            PostAdvices(){
                let para = {
                    page: this.page,
                    size: this.size,
                    data: this.AdvData
                };
                this.listLoading = true;
                advicesList(para).then((res) => {
                    console.log(res)
                    this.listLoading = false;
                    let {code, data} = res;
                    if (code !== 20000) {
                        this.$message({message: res.message, type: 'error'});
                    } else {
                        this.AdvIty = data.content;
                        this.total = data.totalElements;
                    }
                });
            },
            handleAdd(){
                this.addFormVisible = true;
                this.addForm = {
                    title: '',
                    content: ''
                };
                if(this.ueAdd !== null){
                    this.ueAdd.setContent('');
                }
            },
            abolish(){this.editFormVisible = false;this.PostAdvices();},
            handleEdit(index, row){
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                var time = setInterval(() => {
                    if(this.ueEdit!==null){
                        this.ueEdit.setContent(row.content);
                        clearInterval(time);
                    }
                },100)
            },
            handleDel(index, row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {id: row.id};
                    let id = row.id;
                    advicesDel(id, para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: res.message,
                            type: 'success'
                        });
                        this.PostAdvices();
                    });
                });
            },
            addSubmit(a){
                this.$refs[a].validate((valid) => {
                    if(this.ueAdd.getContent()===''){this.$message({message: '请填写内容', type: 'error'});return;}
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = this.addForm;
                            para.content=this.ueAdd.getContent();
                            advicesNew(para).then((res) => {
                                this.addLoading = false;
                                this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                this.addFormVisible = false;
                                this.PostAdvices();
                            });
                        });
                    }
                })
            },
            editSubmit() {
                if(this.editForm.title=== ''){
                    this.$message({message: "请完整的填写信息", type: 'error'});return;
                }
                if(this.ueEdit.getContent()===''){this.$message({message: '请填写内容', type: 'error'});return;}
                this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    let para = {
                        id : this.editForm.id,
                        title : this.editForm.title,
                        content : this.ueEdit.getContent()
                    };
                    advicesEdit(para).then((res) => {
                        this.editLoading = false;
                        this.$message({
                            message: res.message,
                            type: 'success'
                        });
                        this.editFormVisible = false;
                        this.PostAdvices();
                    });
                });
            }
        },
        mounted(){
            this.PostAdvices();
        },
        components:{
            UEditor
        }
    }
</script>
<style type="text/scss" lang="scss" scoped>
    #advices{
        .adv-text{
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
    }
</style>